﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBS.Bll.Tools;
using IBS.Model;
using IBS.Model.Base;

namespace IBS.Bll.KnowledgeBase
{
    public class ClientValidator
    {
        // public const string ERROR_MESSAGE_SAME_PASSPORT_EXISTS = "Client with same passport already exists";
        public const string ERROR_MESSAGE_SAME_CLIENT_EXISTS = "Client with same passport already exists";

        public const string ERROR_MESSAGE_CLIENT_DOES_NOT_EXIST = "Client doesn't exist";

        private IIbsContext context;

        public ClientValidator(IIbsContext context)
        {
            this.context = context;
        }

        public bool ClientWithSamePassportExists(Client clientInfo)
        {
            return context.Clients
                       .Count(
                           c => c.PassportNumber == clientInfo.PassportNumber &&
                                c.PassportSeries == clientInfo.PassportSeries
                       ) > 0;

        }
        public bool CanUpdateClient(Client client)
        {
            var oldPassport = (from c in context.Clients
                              where c.Id == client.Id
                              select new {Number = c.PassportNumber, Series = c.PassportSeries})
                              .FirstOrDefault();
            bool clientUpdatedPassport = (client.PassportNumber != oldPassport.Number ||
                                           client.PassportSeries != oldPassport.Series);
            if (clientUpdatedPassport)
            {
                return ClientWithSamePassportExists(client);
            }
            return true;
        }
        //public bool ClientDoesNotExists(Client clientInfo)
        //{
        //    return ClientWithSamePassportExists(clientInfo);
        //}

        //public bool ClientExists(Client clientInfo)
        //{
        //    return context.Clients.Count(c => c.Id == clientInfo.Id) > 0;
        //}

       
    }
}
